package net.wanho.shop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import net.wanho.shop.po.Supply;
import net.wanho.shop.service.SupplyService;
import net.wanho.shop.vo.AjaxResult;
import net.wanho.shop.vo.PageVo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2025/1/6 17:56
 * @version: 1.0
 */
@Api(tags = "17.供货商接口")
@RequestMapping("/supplies")
@RestController()
@RequiredArgsConstructor
public class SupplyController {

    private final SupplyService supplyService;

    @ApiOperation("订单表分页查询")
    @GetMapping
    public AjaxResult findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
                               @ApiParam("页大小") @RequestParam(defaultValue = "5") int pageSize,
                               @ApiParam("查询条件") Supply supply) {
        PageVo pageVo = supplyService.selectAll(pageNum, pageSize, supply);
        return AjaxResult.success(pageVo);
    }

    @ApiOperation("根据主键查询供货商表")
    @GetMapping("/{id}")
    public AjaxResult findById(@PathVariable Integer id) {
        Supply supply = supplyService.selectById(id);
        return AjaxResult.success(supply);
    }

    @ApiOperation("添加供货商")
    @PostMapping
    public AjaxResult add(@RequestBody Supply supply) {
        supplyService.addSupply(supply);
        return AjaxResult.success();
    }

    @ApiOperation("删除供货商")
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable String id) {
        supplyService.deleteSupply(Integer.valueOf(id));
        return AjaxResult.success("删除成功");
    }

    @ApiOperation("修改供货商表")
    @PutMapping("/{id}")
    public AjaxResult modify(@RequestBody Supply supply, @PathVariable String id) {
        supplyService.updateSupply(supply, Integer.parseInt(id));
        return AjaxResult.success("修改成功", id);
    }

    @ApiOperation("批量删除会员表")
    @DeleteMapping
    public AjaxResult removeByIds(@RequestBody ArrayList<Integer> ids) {
        supplyService.removeByIdList(ids);
        return AjaxResult.success("删除成功", ids);
    }

    @SneakyThrows
    @ApiOperation("会员导出")
    @GetMapping("/export")
    public void export(@ApiParam("页码") @RequestParam(defaultValue = "1") int pageNum,
                       @ApiParam("页大小") @RequestParam(defaultValue = "5") int pageSize,
                       @ApiParam("查询条件") Supply supply, HttpServletResponse response) {
        Workbook workbook = supplyService.export(pageNum, pageSize, supply);

        response.setContentType("application/vnd.ms-excel");

        workbook.write(response.getOutputStream());
    }

    @SneakyThrows
    @ApiOperation("会员导入")
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestBody MultipartFile multipartFile) {
        supplyService.importExcel(multipartFile);
        return AjaxResult.success();
    }
}
